<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "../mixins/resize";

const animationDuration = 6000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    chartdata: {
      type: Array,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      var that = this;
      var data_name = [];
      var data_value = [];
      for (var i = 0; i < that.chartdata.length; i++) {
        data_name[i] = that.chartdata[i].name;
        data_value[i] = that.chartdata[i].cnt;
      }
      this.initChart(data_name, data_value);
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart(data_name, data_value) {
      this.chart = echarts.init(this.$el);
      this.chart.setOption({
        color: ['#1814c4', '#a3a1fb'],
        title: {
          text: "区域安全隐患数量统计分析",
          left: "left",
          padding: [
            10, // 上
            10, // 右
            10, // 下
            10, // 左
          ],
          textStyle: { color: "#606266", fontSize: "14" },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
        },
        grid: {
          top: 60,
          left: "2%",
          right: "2%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: data_name,
            axisTick: {
              show: false
              // alignWithLabel: true,
            },
            axisLine: {
              show: false
            },
            axisLabel: {
              interval: 'auto',
              // rotate: 38,
              textStyle: {
                color: "#606266", //文字的颜色
              },
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            axisTick: {
              show: false,
            },
            axisLabel: {
              textStyle: {
                color: "#606266", //文字的颜色
              },
            },
          },
        ],
        series: [
          {
            name: "隐患数量",
            type: "bar",
            stack: "vistors",
            barWidth: "30px",
            data: data_value,
            label: {
              show: true, //开启显示数值
              position: "top", //数值在上方显示
              textStyle: {
                //数值样式
                color: "#A3A1FB", //字体颜色
                fontSize: 14, //字体大小
              },
            },
            itemStyle: {
              //color:'#A3A1FB',
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                { offset: 0, color: 'rgb(206,204,255)' },
                { offset: 0.7, color: '#8e8ce5' },
                { offset: 1, color: '#a3a1fb' }
              ]),
              borderRadius: [5, 5, 0, 0]
            },
          },
        ],
      });
    },
  },
};
</script>
